import re
import datetime
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.dates import DateFormatter, DayLocator, HourLocator, MinuteLocator, date2num, num2date

date = []
value = []
pattern = re.compile(r'.*"illuminance_lux":"(\d+)"')

def getdatetime(matchResult):
    rawtime = matchResult.group(0)[:20]
    rawtime = rawtime.strip('[]')
    return "2025-" + rawtime


with open("/home/lhy/ownproject/code/01_python/other/zigbee_light_log.txt") as file:
    lines = file.readlines()
    for line in lines:
        matchResult = pattern.match(line)
        if matchResult:
            date.append(getdatetime(matchResult))
            value.append(matchResult.group(1))
            print("{}   {}".format(getdatetime(matchResult), matchResult.group(1)))


# # 创建日期时间数组
# dates = [datetime.datetime.fromisoformat(str) for str in date]
# times = [date2num(d) for d in dates]  # 将datetime对象转换为Matplotlib可读的格式
# values = np.array(value)

# # 绘制曲线图
# plt.plot(times, values, 'o')
# plt.gca().xaxis.set_major_formatter(DateFormatter('%H'))  # 设置日期时间格式
# plt.gca().xaxis.set_major_locator(HourLocator())  # 设置小时刻度间隔

# # plt.yticks(np.arange(0, 1000, 100)) # 设置纵坐标范围和间隔
# plt.autoscale(enable=True, axis='y', tight=True)


# plt.title('Time Data Curve with Dates')
# plt.xlabel('Date')
# plt.ylabel('Value')
# plt.grid(True)  # 显示网格
# plt.show()

